package driver.actions;

import java.io.IOException;
import java.net.Socket;

import messages.persondispatcher.RegisterHallCallMessage;
import state.Globals;
import state.group.HallCall;
import system.GlobalData;

/**
 * This is a Hall Call action, used to send a hall call to the Group Dispatcher
 * 
 * @author Ian Doerr
 */
public class HallCallAction extends BaseAction {

    private HallCall call;
    
    /**
     * Initializes this action with the given call.
     * 
     * @param call
     */
    public HallCallAction(HallCall call)
    {
        this.call = call;
    }
    
    @Override
    /**
     * Sends out the Hall Call to the Group Dispatcher
     */
    public void performAction()
    {
        RegisterHallCallMessage message = new RegisterHallCallMessage(call);
        
        Socket sock = GlobalData.elevatorThread.getRemoteSocket();
        
        if (!sock.isClosed())
        {
            try
            {
                sock.getOutputStream().write(message.toByteArray());
            }
            catch (IOException e)
            {
                Globals.getLogger().error("Error writing out the message: " + message, e);
            }
        }
    }
    
    @Override
    /**
     * Returns a representation of this action
     */
    public String toString()
    {
        return "<HallCallAction call=" + call.toString() + "/>";
    }

}
